<?php

/**
 * This is the model class for table "{{vendor}}".
 *
 * The followings are the available columns in table '{{vendor}}':
 * @property integer $vendorID
 * @property integer $adminID
 * @property string $vendor_name
 * @property string $vendor_description
 * @property integer $vendor_cityID
 * @property integer $vendor_stateID
 * @property string $vendor_address
 * @property string $vendor_type
 * @property string $subscription_email
 * @property string $vendor_logo_file
 * @property string $vendor_logo_thumb
 * @property string $vendor_bg_image
 * @property string $created
 * @property string $modified
 *
 * The followings are the available model relations:
 * @property BannerMgmt[] $bannerMgmts
 * @property Competition[] $competitions
 * @property Image[] $images
 * @property Job[] $jobs
 * @property JobApplication[] $jobApplications
 * @property Offer[] $offers
 * @property OfferSuggestion[] $offerSuggestions
 * @property Poll[] $polls
 * @property RecruitedCnt[] $recruitedCnts
 * @property Subscription[] $subscriptions
 * @property Survey[] $surveys
 * @property TransactionDetail[] $transactionDetails
 * @property Admin $admin
 * @property City $vendorCity
 * @property State $vendorState
 * @property VendorStore[] $vendorStores
 * @property Video[] $videos
 */
class Vendor extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Vendor the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{vendor}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('vendor_name, vendor_cityID, vendor_stateID, vendor_countryID,zoneID, microsite,package, period, start_date, end_date,keywords, featured, vendor_type,vendor_description,contact_person,mobile_no_1, vendor_address', 'required'),
			array('vendor_name', 'required','on'=>array('create')),
			array('adminID, vendor_cityID, vendor_stateID', 'numerical', 'integerOnly'=>true),
			array('vendor_name, subscription_email, vendor_logo_file, vendor_logo_thumb, vendor_bg_image', 'length', 'max'=>100),
			array('vendor_address', 'length', 'max'=>400),
			array('vendor_type', 'length', 'max'=>70),
			array('vendor_description, created, modified', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('vendorID, adminID, vendor_name, vendor_description, vendor_cityID, status, vendor_stateID, vendor_address, vendor_type, subscription_email, vendor_logo_file, vendor_logo_thumb, sortOrder, vendor_bg_image, created, modified', 'safe', 'on'=>'search'),
			array('modified','default',
           	  'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'update'),
        	array('created,modified','default',
              'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'insert'),			  
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'bannerMgmts' => array(self::HAS_MANY, 'BannerMgmt', 'vendorID'),
			'competitions' => array(self::HAS_MANY, 'Competition', 'vendorID'),
			'images' => array(self::HAS_MANY, 'Image', 'vendorID'),
			'jobs' => array(self::HAS_MANY, 'Job', 'vendorID'),
			'jobApplications' => array(self::HAS_MANY, 'JobApplication', 'vendorID'),
			'offers' => array(self::HAS_MANY, 'Offer', 'vendorID'),
			'offerSuggestions' => array(self::HAS_MANY, 'OfferSuggestion', 'vendorID'),
			'polls' => array(self::HAS_MANY, 'Poll', 'vendorID'),
			'recruitedCnts' => array(self::HAS_MANY, 'RecruitedCnt', 'vendorID'),
			'subscriptions' => array(self::HAS_MANY, 'Subscription', 'vendorID'),
			'surveys' => array(self::HAS_MANY, 'Survey', 'vendorID'),
			'transactionDetails' => array(self::HAS_MANY, 'TransactionDetail', 'vendorID'),
			'admin' => array(self::BELONGS_TO, 'Admin', 'adminID'),
			'vendorCity' => array(self::BELONGS_TO, 'City', 'vendor_cityID'),
			'vendorState' => array(self::BELONGS_TO, 'State', 'vendor_stateID'),
			'vendorStores' => array(self::HAS_MANY, 'VendorStore', 'vendorID'),
			'videos' => array(self::HAS_MANY, 'Video', 'vendorID'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'vendorID' => 'Vendor',
			'adminID' => 'Admin',
			'vendor_name' => 'BRAND NAME',
			'vendor_description' => 'BRAND DESCRIPTION/OVERVIEW',
			'vendor_cityID' => 'CITY',
			'vendor_stateID' => 'STATE',
			'vendor_countryID' => 'COUNTRY',
			'vendor_address' => 'BRAND ADDRESS',
			'contact_person' =>'CONTACT PERSON NAME',
			'contact_designation' =>'DESIGNATION',
			'vendor_type' => 'CATEGORY',
			'status'=>'STATUS',
			'subscription_email' => 'SUBSCRIPTION EMAIL',
			'vendor_logo_file' => 'BRAND LOGO',
			'vendor_logo_thumb' => 'BRAND LOGO THUMB',
			'vendor_bg_image' => 'BRAND BACKGROUND IMAGE',
			'categoryID'=>'CATEGORY',
			'sortOrder'=>'SORT ORDER',
			'is_top_recruiter'=>'TOP RECRUITER',
			'is_top_brand'=>'TOP BRAND',
			'created' => 'CREATED',
			'modified' => 'MODIFIED',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->compare('vendorID',$this->vendorID);
		$criteria->compare('adminID',$this->adminID);
		$criteria->compare('vendor_name',$this->vendor_name,true);
		$criteria->compare('vendor_description',$this->vendor_description,true);
		$criteria->compare('vendor_cityID',$this->vendor_cityID);
		$criteria->compare('vendor_stateID',$this->vendor_stateID);
		$criteria->compare('vendor_address',$this->vendor_address,true);
		$criteria->compare('vendor_type',$this->vendor_type,true);
		$criteria->compare('subscription_email',$this->subscription_email,true);
		$criteria->compare('sortOrder',$this->sortOrder,true);
		$criteria->compare('vendor_logo_file',$this->vendor_logo_file,true);
		$criteria->compare('vendor_logo_thumb',$this->vendor_logo_thumb,true);
		$criteria->compare('vendor_bg_image',$this->vendor_bg_image,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
    		    //'defaultOrder'=>'sortOrder ASC',
    		),
		));
	}
	
	public function getModel($type,$arrParams = array())
	{
		switch ($type)
		{
			case 'ADMIN-ID':
			 $obj=Vendor::model()->find('adminID=?', array($arrParams['adminID']))	;
			//echo $arrParams['adminID'];print_r($obj);exit;
			
			return $obj;
			break;
			
			case 'VENDOR-ID':
			return Vendor::model()->findByPk($arrParams['vendorID']) ;
			break;
			
			case 'VENDOR-LIKE':
				return Vendor::model()->find(array(
					'condition' => "vendor_name LIKE :match",      
					'params'    => array(':match' => "%".$arrParams['vendor_name']."%")
				));
			break;
			
			case 'VENDOR-NAME':
			$value = Vendor::model()->find(array('select'=>'vendorID','condition'=>"vendor_name LIKE '%".$arrParams['vendorName']."%' AND status=1")) ;
			if(!empty($value))
			return $value->vendorID;
			else 
			return null;
			break;                   
			                                                                                                                           
			case 'VENDOR-NAME-BY-ID':
				$vendorobj = Vendor::model()->findByPk($arrParams['vendorID']);
			if (!empty($vendorobj))
			return $vendorobj->vendor_name;
			else 
			return null;
			break;
                        
                        case 'VENDOR-SLUG':
			return Vendor::model()->find('slug=?',array($arrParams['slug'])) ;
			break;
		}
	
	}
	
	public function getSwitch($switch)
	{  //echo"hi";echo $switch;exit;
		if($switch == 1) {
			$value = "ENABLE";
	    }else if($switch == 0) {
			$value = "DISABLE";
	    } 
	    return $value ; 
	}
	
	public static function getList($type,$arrParams = array())
	{
		switch ($type)
		{
			case 'ALL':
			return Vendor::model()->findAll();
			break;
					
			case 'VENDOR-ENABLE':
			return Vendor::model()->findAll(array('condition'=>'status=1'));
			break;

			case 'VENDOR-TYPE':
			return Vendor::model()->findByPk((int)$arrParams['vendorID']) ;
			break;
			
			case 'CATEGORY':
				$condition = "";
			if ($arrParams['keyword'] !="" && $arrParams['keyword']!="Keywords...")
				$condition = " AND vendor_name LIKE '%".$arrParams['keyword']."%' and status = 1 ";

				
			if ($arrParams['categoryID']!="" && $arrParams['categoryID']!="5")
				{
					if ($condition!="")
					$condition .= " AND vendor_type = ".$arrParams['categoryID'];
					else 
					$condition .= "vendor_type = ".$arrParams['categoryID'];
				}
				
			if ($arrParams['location'] =="" || $arrParams['location']=="Select City")
			{
				if ($condition!="")
					$condition .= " AND status = 1";
					else 
					$condition .= "status = 1";
				return Vendor::model()->findAll($condition);
				
			}
			else
			{
				if (!empty($condition))
			return Vendor::model()->with('vendorStores')->findAll($condition.' AND store_cityID=? AND status=1',array($arrParams['search_location_cityID_hidden']));
			else 
			return Vendor::model()->with('vendorStores')->findAll('store_cityID=? AND status=1',array($arrParams['search_location_cityID_hidden']));
			
			}
			break;
			
			case 'CATEGORY-All':
				$condition = "";
			if ($arrParams['keyword'] !="" && $arrParams['keyword']!="Keywords...")
				$condition = "vendor_name LIKE '%".$arrParams['keyword']."%' and status = 1 ";
				
			if ($arrParams['location'] =="" || $arrParams['location']=="Select City")
			{
				if ($condition!="")
					$condition .= " AND status = 1";
					else 
					$condition .= "status = 1";
					
			return Vendor::model()->findAll($condition);
			}
			else
			{
				if (!empty($condition))
			return Vendor::model()->with('vendorStores')->findAll($condition.' AND store_cityID=? AND status=1',array($arrParams['search_location_cityID_hidden']));
			else 
			return Vendor::model()->with('vendorStores')->findAll('store_cityID=? AND status=1',array($arrParams['search_location_cityID_hidden']));
			
			}
			break;
			
			case 'BRAND-SEARCH':				
				
				$data = $arrParams['data'];
				$condition = "";
				
				if ($data['keyword'] !="" && $data['keyword']!="Keywords...")
				{
					$condition = "vendor_name LIKE '%".$data['keyword']."%'  ";
				}
				if ($data['job_type']!="" && $data['job_type']!="5")
				{
					
					if ($condition!="")
					$condition .= " AND vendor_type = ".$data['job_type'];
					else 
					$condition .= "vendor_type = ".$data['job_type'];
				}

				if ($data['location']!="" && $data['location']!="Select City")
				{					
					if ($condition!="")
					return Vendor::model()->with('vendorStores')->findAll($condition.' AND store_cityID=? and status = 1',array($data['search_location_cityID_hidden']));
					else 
					return Vendor::model()->with('vendorStores')->findAll('store_cityID=? and status=1',array($data['search_location_cityID_hidden']));
				}
				else 
				{
					if ($condition!="")
					$condition .= " AND status = 1";
					else 
					$condition .= "status = 1";
					
					return Vendor::model()->findAll($condition,array(),array('order'=>'created')) ;
				}
			
			break;
			
			
			case 'ALL-RAND':
				return Vendor::model()->findAll(array('select'=>'*, rand() as rand','limit'=>'20','order'=>'rand')) ;
				break;
			case 'TOP-BRANDS':
			return Vendor::model()->findAll(array('condition'=>'is_top_brand=1  and status = 1 ')) ;
			break;
			
			case 'ALL-PAGES':
			return Vendor::model()->findAll() ;
			break;
			
			case 'BRAND-CLOUD':
			return Vendor::model()->findAll('brand_cloud=1') ;
			break;

		}
	}
}